<?php
// Library files
include 'includes/cDates.php';
include 'includes/cPlayer.php';
include 'includes/cMember.php';
include 'includes/cDatabase.php';
include 'includes/variables.php';
include 'includes/cPage.php';

// Globals
define("TD_POINTS", "<td width='100' align='center'>");
define("TD_NAME", "<td width='300' align='left'>");
define("TD_NORMAL", "<td width='100' align='center'>");

$cPage = new page("Scoreboard");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

// ******************************************************************* START HERE
$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'memberlist', $selectArray, NULL, NULL, NULL, NULL);

$finfo = mysqli_fetch_fields($result);
	
$cDates = new dates;
$currentWeek = $cDates->getCurrentWeek();

for ($i = 0; $i < mysqli_num_rows($result); $i++)	// Do each member's team at a time
{
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	
	// Get all the data for this member and put it in the class
	$cMember[$i] = new member;

	$error |= $cMember[$i]->setMemberName($row['memberName']);
	$error |= $cMember[$i]->setTeamName($row['teamName']);
	$error |= $cMember[$i]->setMoneyPaid($row['moneyPaid']);
	$error |= $cMember[$i]->setActive($row['active']);
	$error |= $cMember[$i]->loadMemberPlayerInfo($row, $connect);
}

sortMemberScores($cDates, $cMember, mysqli_num_rows($result), $currentWeek);
displayScoreBoard($cMember, mysqli_num_rows($result), $currentWeek);

function sortMemberScores($cDates, $cMember, $numMembers, $currentWeek)
{
	$scoreLastWeekArray = array();
	$scoreLastLastWeekArray = array();
	$flag = 0;
	
	for ($i = 0; $i < $numMembers; $i++)	// Do each member's team at a time
	{
		$scoreLastWeekArray[$i] = $cMember[$i]->getMemberTotalScore() /*- $cMember[$i]->getScoreForWeek($currentWeek-1)*/;
		$scoreLastLastWeekArray[$i] = $cMember[$i]->getMemberTotalScore() - $cMember[$i]->getScoreForWeek($currentWeek-1)/* - $cMember[$i]->getScoreForWeek($currentWeek-2)*/;
		//echo "<BR>".$cMember[$i]->getMemberName()." last ".$cMember[$i]->getScoreForWeek($currentWeek-2)." this ".$cMember[$i]->getScoreForWeek($currentWeek-1);
	}
	rsort($scoreLastWeekArray);		// Sort in reverse order
	rsort($scoreLastLastWeekArray);		// Sort in reverse order
	
	// We now have all the scores in order but we need to relate them to each memberTeam
	for ($i = 0; $i < $numMembers; $i++)
	{
		$totalPointsLastWeek = $cMember[$i]->getMemberTotalScore() /*- $cMember[$i]->getScoreForWeek($currentWeek-1)*/;
		for ($j = 0; $j < sizeof($scoreLastWeekArray); $j++)
		{	
			if ($totalPointsLastWeek == $scoreLastWeekArray[$j])
			{
				if (!$flag)
					$cMember[$i]->setPosition(($j + 1), $currentWeek-1);	// Add one as this array starts at 0
				$flag++;
			}
		}
		$cMember[$i]->setMatchingScore($flag, $currentWeek-1);	// Indicates how many other members are on the same score
		$flag = 0;										// reset flag, it's a new member
	}
	$flag = 0;
	for ($i = 0; $i < $numMembers; $i++)
	{
		$totalPointsLastLastWeek = $cMember[$i]->getMemberTotalScore() - $cMember[$i]->getScoreForWeek($currentWeek-1) /*- $cMember[$i]->getScoreForWeek($currentWeek-2)*/;
		for ($j = 0; $j < sizeof($scoreLastLastWeekArray); $j++)
		{	
			if ($totalPointsLastLastWeek == $scoreLastLastWeekArray[$j])
			{
				if (!$flag)
					$cMember[$i]->setPosition(($j + 1), $currentWeek-2);	// Add one as this array starts at 0
				$flag++;
			}
		}
		$cMember[$i]->setMatchingScore($flag, $currentWeek-2);	// Indicates how many other members are on the same score
		$flag = 0;										// reset flag, it's a new member
	}
}

function displayScoreBoard ($cMember, $numMembers, $currentWeek)
{	
	$lastWeek = $currentWeek - 2;
	$thisWeek = $currentWeek - 1;
	
	if ($thisWeek < 0)
	{
		$thisWeek = 0;
		$lastWeek = 0;
	}
	
	echo "<div align='center'>";
	echo "<table>";
	echo TD_NORMAL."<strong>Position This Week(".$thisWeek.")</strong></td>";
	echo TD_NORMAL."<strong>Position Last Week(".$lastWeek.")</strong></td>";
	echo TD_NAME."<strong>Team Name</strong></td>";
	echo TD_NAME."<strong>Member Name</strong></td>";
	echo TD_POINTS."<strong>Total Points</strong></td>";
	echo TD_POINTS."<strong>Points This Week(".$thisWeek.")</strong></td>";
	echo TD_POINTS."<strong>Prize Money</strong></td>";
	for ($position = 1; $position <= $numMembers; $position++)
	{
		if ($position < 6)
			$colour = RED;
		else
			$colour = BLUE;
		// Find members from first to last being careful with members on the same points
		for ($memberIndex = 0; $memberIndex < $numMembers; $memberIndex++)
		{
			if ($cMember[$memberIndex]->getPosition($thisWeek) == $position)
			{	// We've found it
				echo "<tr>".TD_NORMAL.$colour.$cMember[$memberIndex]->getPosition($thisWeek);
				if ($cMember[$memberIndex]->getMatchingScore($thisWeek) > 1)
				{
					echo "=";
				}
				echo "</font></td>";
				echo TD_NORMAL.$colour.$cMember[$memberIndex]->getPosition($lastWeek);
				if ($cMember[$memberIndex]->getMatchingScore($lastWeek) > 1)
				{
					echo "=";
				}
				echo "</font></td>";
				echo TD_NAME.$colour.$cMember[$memberIndex]->getTeamName()."</font></td>";
				echo TD_NAME.$colour.$cMember[$memberIndex]->getMemberName()."</font></td>";
				echo TD_POINTS.$colour.$cMember[$memberIndex]->getMemberTotalScore()."</font></td>";
				echo TD_POINTS.$colour.$cMember[$memberIndex]->getScoreForWeek($thisWeek)."</font></td>";
				if ($position < 6)
				{
					echo TD_POINTS.$colour."£".getPrizeMoney($position, $numMembers, $cMember, $currentWeek)."</font></td>";
				}
				echo "</tr>";
			}
		}
	}
	echo "</table></div>";
}

function getPrizeMoney($position, $numMembers, $cMember, $currentWeek)
{
	$totalFund = 0;
	$membersOnThisPosition = 0;
	
	for ($i = 0; $i < $numMembers; $i++)
	{
		$totalFund += $cMember[$i]->getMoneyPaid();
	}
	// Now remove the amounts payable for team of the month
	$totalFund -= (TEAM_OF_THE_MONTH * 10);
	
	// Remove ERICSSON Cup prize money
	$totalFund -= TANDBERG_CUP;
	
	// Need to determine how many members are on this position
	for ($memberIndex = 0; $memberIndex < $numMembers; $memberIndex++)
	{
		if ($cMember[$memberIndex]->getPosition($currentWeek-1) == $position)
		{
			$membersOnThisPosition++;
		}
	}
	
	switch ($position)
	{
		case 1	:	if ($membersOnThisPosition == 1)
						$prizeMoney = $totalFund * FIRST_PLACE_RATIO;
					else if ($membersOnThisPosition == 2)
						$prizeMoney = (($totalFund * (FIRST_PLACE_RATIO + SECOND_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition == 3)
						$prizeMoney = (($totalFund * (FIRST_PLACE_RATIO + SECOND_PLACE_RATIO + THIRD_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition == 4)
						$prizeMoney = (($totalFund * (FIRST_PLACE_RATIO + SECOND_PLACE_RATIO + THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition > 4)
						$prizeMoney = $totalFund / $membersOnThisPosition;
					break;
		case 2	:	if ($membersOnThisPosition == 1)
						$prizeMoney = $totalFund * SECOND_PLACE_RATIO;
					else if ($membersOnThisPosition == 2)
						$prizeMoney = (($totalFund * (SECOND_PLACE_RATIO + THIRD_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition == 3)
						$prizeMoney = (($totalFund * (SECOND_PLACE_RATIO + THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition == 4)
						$prizeMoney = (($totalFund * (SECOND_PLACE_RATIO + THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO + FIFTH_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition > 4)
						$prizeMoney = (($totalFund * (SECOND_PLACE_RATIO + THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO + FIFTH_PLACE_RATIO)) / $membersOnThisPosition);
					break;
		case 3	:	if ($membersOnThisPosition == 1)
						$prizeMoney = $totalFund * THIRD_PLACE_RATIO;
					else if ($membersOnThisPosition == 2)
						$prizeMoney = (($totalFund * (THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition == 3)
						$prizeMoney = (($totalFund * (THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO + FIFTH_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition > 3)
						$prizeMoney = (($totalFund * (THIRD_PLACE_RATIO + FOURTH_PLACE_RATIO + FIFTH_PLACE_RATIO)) / $membersOnThisPosition);
					break;
		case 4	:	if ($membersOnThisPosition == 1)
						$prizeMoney = $totalFund * FOURTH_PLACE_RATIO;
					else if ($membersOnThisPosition == 2)
						$prizeMoney = (($totalFund * (FOURTH_PLACE_RATIO + FIFTH_PLACE_RATIO)) / $membersOnThisPosition);
					else if ($membersOnThisPosition > 2)
						$prizeMoney = (($totalFund * (FOURTH_PLACE_RATIO + FIFTH_PLACE_RATIO)) / $membersOnThisPosition);
					break;
		case 5	:	if ($membersOnThisPosition == 1)
						$prizeMoney = $totalFund * FIFTH_PLACE_RATIO;
					else if ($membersOnThisPosition > 1)
						$prizeMoney = (($totalFund * FIFTH_PLACE_RATIO) / $membersOnThisPosition);
					break;
		default	:	$prizeMoney = 0;
					break;
	}
		
	return round ($prizeMoney, 2);
}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>